@isTest
private class AppendReagendProductControllerTest {
    static testMethod void testMethod1() {

    	Id ldcProfile = [Select Id From Profile Where Name ='系统管理员' or Name='System Administrator'].Id;
        User u1 = new User( Alias = 'samLdc1', 
                         Email = 'ldc@test.com.maike',
                         Emailencodingkey = 'UTF-8', 
                         Lastname = '用户1', 
                         Languagelocalekey = 'en_US',
                         Localesidkey = 'en_US', 
                         Profileid = ldcProfile, 
                         Country = 'United States',
                         IsActive = true,
                         SubCompany__c='四川省迈克实业有限公司',
                         UserDepartment__c='客户服务中心(工程部)',
                         Timezonesidkey = 'America/Los_Angeles', 
                         EmployeeNumber = '123451',
                         Username = 'Matldc1@test.com.maike' );
        insert u1;
       
        
        // 客户
        Account acc = TestUtils.createAccount();
        acc.BillingType__c = '专票';
        insert acc;
		
        
        // 业务机会
        Opportunity opp = TestUtils.createOpportunity();
        opp.AccountId = acc.Id;
        insert opp;

        
		ProductStructure__c ps2 = new ProductStructure__c();
        ps2.Classify3__c = '试剂';
        insert ps2;
        // 产品
        Product__c p = TestUtils.createProduct();
        p.bggsjhdj__c = 10.0;
        p.zl__c = '12.5ml';
        p.Purpose__c = '试剂报价用';
        p.ProductStructure__c = ps2.Id;
        
        Product__c p2 = TestUtils.createProduct();
        p2.Name = 'aaaa';
        p2.bggsjhdj__c = 10.0;
        p2.zl__c = '100ml';
        p2.ProductStructure__c = ps2.Id;
        
        Product__c p3 = TestUtils.createProduct();
        p3.Name = '测试合同产品';
        p3.bggsjhdj__c = 10.0;
        p3.zl__c = '200ml';
        p3.ProductStructure__c = ps2.Id;
        insert new Product__c[] {p, p2,p3};

      

        // 价格
        Price__c price = TestUtils.createPrice();
        price.Product__c = p.Id;
        price.Company__c = '四川省迈克实业有限公司';
        price.InstCost__c = 10.0;
        price.InvoiceDealerPrice__c = 10.0;
        price.Taxrate__c = 0.03;
        price.MarketPrice__c = 10.0;
        
        Price__c price2 = TestUtils.createPrice();
        price2.Product__c = p2.Id;
        price2.Company__c = '四川省迈克实业有限公司';
        price2.InstCost__c = 10.0;
        price2.InvoiceDealerPrice__c = 10.0;
        price2.Taxrate__c = 0.17;
        price2.MarketPrice__c = 10.0;
        
        Price__c price3 = TestUtils.createPrice();
        price3.Product__c = p3.Id;
        price3.Company__c = '四川省迈克实业有限公司';
        price3.InstCost__c = 10.0;
        price3.InvoiceDealerPrice__c = 10.0;
        price3.Taxrate__c = 0.17;
        price3.MarketPrice__c = 10.0;
        insert new Price__c[] {price, price2,price3};
        
            /// 日期处理
        Integer m = Date.today().month(); //当前月份
        Integer y = Date.today().year();  //当前年份 
       ///取上月
         if(m == 1){
        m = 12 ;
            y -= 1;
        }else{
        m -= 1;
            
        }
        
        String  month = String.valueOf(m) + '月';
        String  year= String.valueOf(y) + '年';
        AveragePrice__c av1 = new AveragePrice__c(
        Product__c = p.Id,
            Company__c = '四川省迈克实业有限公司',
            AveragePrice__c = 1.0,
            Year__c = year,
            Month__c = month
            
        );
          AveragePrice__c av2 = new AveragePrice__c(
       		 Product__c = p2.Id,
            Company__c = '四川省迈克实业有限公司',
            AveragePrice__c = 1.0,
            Year__c = year,
            Month__c = month
            
        );
        insert new AveragePrice__c[] {av2,av1};
        
        ///合同
        Contract__c c = new Contract__c();
        c.name = '测试合同';
        c.Account__c = acc.Id;
        c.Status__c= '已生效';
        c.EndDate__c = Date.newInstance(2099, 1, 1);
        insert c;
        
        //报价2
        Quote qt2 = new Quote(Name = '试剂报价',Type__c = '试剂报价',OpportunityId = opp.Id);
        insert qt2;

        //报价产品1
        QuoteProduct__c qp1 = new QuoteProduct__c(Name = '测试报价产品1',Product__c = p.Id,Quote__c = qt2.Id);        
        insert qp1;


        Measure_Calculate__c mc = new Measure_Calculate__c(
            Opportunity__c = opp.Id,
            Company__c = '四川省迈克实业有限公司',
            Status__c = '审批成功'
        );
        insert mc;


        Measure_Calculate_Product__c mcp = new Measure_Calculate_Product__c(
            Measure_Calculate__c = mc.Id,
            Product__c = p.Id
        );
        insert mcp;

       


        PageReference page = new PageReference('/apex/AppendReagendProductController?cid=' + c.Id );
        System.Test.setCurrentPage(page);
        AppendReagendProductController arp = new AppendReagendProductController();
        arp.oppId = opp.Id;
        arp.qid = qt2.Id;
        arp.init();
        arp.selectedCon = c.Id;
        arp.save();

        arp.qid = null ;
        arp.mcid = mc.Id;
        arp.save();

        
               
    }
}